package com.anhdt.dao;

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.slf4j.LoggerFactory;

public final class ConnectDatabase {

    private final org.slf4j.Logger LOG = getLog();
    private DataSource ds;
    private static ConnectDatabase ConnectDatabase;

    public static ConnectDatabase getInstance() {
        if (ConnectDatabase == null) {
            ConnectDatabase = new ConnectDatabase();
        }
        return ConnectDatabase;
    }

    private ConnectDatabase() {
        try {
            InitialContext ctx = new InitialContext();
            this.ds = (DataSource) ctx.lookup("java:comp/env/jdbc/sharetoleandb");
            LOG.info("Start inint DBpool");
        } catch (NamingException ex) {
            LOG.info(ex.getLocalizedMessage());
        }
    }

    public org.slf4j.Logger getLog() {
        return LoggerFactory.getLogger(this.getClass());
    }

    public final Connection getConnnection() throws SQLException {
        return ds.getConnection();
    }
}
